package cn.farm.farm.Service.CommonService;

import cn.farm.farm.Config.Common.CommonDto.PageHelperResult;
import cn.farm.farm.Config.Common.CommonDto.PestExecution;
import cn.farm.farm.Config.Common.CommonDto.Result;
import cn.farm.farm.Config.Common.CommonEnums.PestStateEnum;
import cn.farm.farm.Config.Common.CommonException.LikeException;
import cn.farm.farm.Config.Common.CommonException.NotFoundException;
import cn.farm.farm.Entity.Common.Peststress;
import cn.farm.farm.Mapper.CommonMapper.PestMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PestService {
    @Autowired
    private PestMapper pestMapper;

    public Result<PageHelperResult> foundByName(String name, Integer pageNum, Integer pageSize) {
        //声明数据变量
        Result<PageHelperResult> result;//返回信息
        PestExecution execution;//返回Data
        PageHelperResult<Peststress> pageHelperResult = new PageHelperResult<>();
        Integer Size;
        try{
            //查询所有数据
            List<Peststress> list = pestMapper.likeSelect(name);
            Integer all = list.size();

            //查询数据库
            if(pageNum<=(Integer)(all/pageSize)){
                Size = pageSize;
            }else{
                Size = all - (pageNum-1)*pageSize;
            }
            pageHelperResult.setList(list);
            pageHelperResult.setNumber(all);
            pageHelperResult.setName(name);
            pageHelperResult.setPageNumber(Size);
            if(list.size() == 0){
                throw new LikeException("查询不到任何信息");
            }

            //构造返回变量
            execution = new PestExecution(PestStateEnum.SUCCESS);
            result = new Result(execution.getState(),pageHelperResult,execution.getStateInfo());

            return result;
        }catch(LikeException l){
            execution = new PestExecution(PestStateEnum.FOUND_NULL);
           // result = new Result(execution.getState(),pageHelperResult,execution.getStateInfo());
//不清楚前端怎么写的没有交流好，返回结果中pageHelperResult不知道要不要加
            result = new Result(execution.getState(),execution.getStateInfo());
            return result;
        }
    }

    public PageHelperResult foundByCate(String species, Integer pageNum, Integer pageSize) {
        //1.建立所需变量
        PageHelperResult<Peststress> pests = new PageHelperResult();//具体的植物信息，以及总的植物条数
        List<Peststress> list;
        Integer num;
        Integer Size;
        try{
                num = pestMapper.selectByCate(species);
            if(num == 0){
                throw new NotFoundException("num为空");
            }
            //3.分页助手设置数据显示的页数和每页显示条数
            if(pageNum<=(Integer)(num/pageSize)){
                Size = pageSize;
            }else{
                Size = num - (pageNum-1)*pageSize;
            }
            PageHelper.startPage(pageNum,pageSize);
            //查询数据库
                list = pestMapper.selectByCategory(species);
            pests.setList(list);
            pests.setName(species);
            pests.setNumber(num);
            pests.setPageNumber(Size);
            //4.查询plant表并封装对象
            return pests;
        }catch (NotFoundException n){
            throw new NotFoundException("NotFound0sd inner error:" + n.getMessage());
        }
    }
}
